package com.hibernate.batch;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.junit.Test;

import com.hibernate.domain.Student;
import com.hibernate.utils.HibernateUtil;

/**
 * hibernate之批量插入测试类
 * @author LC
 *
 */
public class SaveBatchTest extends HibernateUtil {
	
	/**
	 * 批量插入数据测试
	 */
	@Test
	public void test_save_batch() {
		Session session = sessionFactory.openSession() ;
		Transaction transaction = session.beginTransaction() ;
		
		for (int i = 0; i < 200; i++) {
			Student student = new Student() ;
			student.setSname("stu-" + i) ;
			student.setDescription("batch-" + i) ;
			session.save(student) ;
			if(i % 50 == 0) {
				session.flush() ;	//生成sql语句
				session.clear() ;	//清除缓存
			}
		}
		
		transaction.commit() ;
		session.close() ;
	}
	
}
